<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="所属建筑" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buildingId">
              <a-select v-model="model.buildingId" placeholder="请选择所属建筑(场地)"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in buildingDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="教室编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomCode">
              <a-input v-model="model.classroomCode" placeholder="请输入教室编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="教室名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomName">
              <a-input v-model="model.classroomName" placeholder="请输入教室名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="教室别名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomNickname">
              <a-input v-model="model.classroomNickname" placeholder="请输入教室别名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="容纳学生数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomStudentCapacity">
              <a-input-number v-model="model.classroomStudentCapacity" placeholder="请输入容纳学生数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="容纳班级数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomClassesCapacity">
              <a-input-number v-model="model.classroomClassesCapacity" placeholder="请输入容纳班级数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="面积（㎡）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomArea">
              <a-input-number v-model="model.classroomArea" placeholder="请输入教室面积" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="教室类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomType">
              <j-dict-select-tag @change="handleChanegObjType" type="list" v-model="model.classroomType" dictCode="classroom_type" placeholder="请选择教室类型" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row style='padding-top: 10px;border-top: 1px solid #e3e3e3;'>
          <a-col :span="12">
            <a-form-model-item label="用途" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomUsage">
              <a-input v-model="model.classroomUsage" placeholder="请输入用途"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="教室结构" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomStructure">
              <a-input v-model="model.classroomStructure" placeholder="请输入教室结构"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomOrder">
              <a-input-number v-model="model.classroomOrder" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否可用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomIzOpen">
<!--              <a-input-number v-model="model.classroomIzOpen" placeholder="请输入是否可用" style="width: 100%" />-->
              <a-radio-group @change="onChangeMenuType" v-model="model.classroomIzOpen">
                <a-radio :value="1">是（可用）</a-radio>
                <a-radio :value="2">否（不可用）</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomIntroduce">
              <a-input v-model="model.classroomIntroduce" placeholder="请输入描述"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomRemark">
              <a-input v-model="model.classroomRemark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {getBuildingDataList} from '@api/commonAPI'

  export default {
    name: 'FacClassroomForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           tenantId: [
              { required: true, message: '请输入学校编码（租户ID）!'},
           ],
        },
        url: {
          add: "/classroom/facClassroom/add",
          edit: "/classroom/facClassroom/edit",
          queryById: "/classroom/facClassroom/queryById"
        },
        //获取下拉选择建筑数据
        buildingDataList: [],
        buildingId: '',
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getBuildingData();
    },
    methods: {
      add () {
        this.modelDefault.classroomIzOpen = 1,//默认值启用（1），未启用（0）
        this.modelDefault.handleChanegObjType = "1",//
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      async getBuildingData() {
        let res = await getBuildingDataList()
        this.buildingDataList = []
        res.result.forEach(item => {
          this.buildingDataList.push({
            label: item.buildingName,
            value: item.id
          })
        })
        console.log(res)
        console.log(this.buildingDataList)
      },
      // 维护浇筑-教室关系方法
      async relBuildingClassroomData() {
        let params = {
          buildingId : this.buildingId
        }
        let res = await relBuildingClassroom(params)
        if (res && res.code === 200){
        }
        console.log("我是维护关系数据提交")
      },
      submitForm () {
        this.relBuildingClassroomData();
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>